<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class article extends a_base {

    public $classpropertyid = 2;

    public function __construct() {
        parent::__construct();
        $this->load->model('m_article');
        $this->load->model('m_class');
    }

    public function index() {
        $orderkey_list = array('articleid', 'classid', 'title', 'configid', 'clicknum', 'listid', 'adminid', 'addtime', 'isclose');
        $orderkey = $this->input->get('orderkey');
        if (!in_array($orderkey, $orderkey_list)) {
            $orderkey = 'articleid';
        }
        $ascdesc = $this->input->get('ascdesc');
        if ($ascdesc != 'asc') {
            $ascdesc = 'desc';
        }
        $where = array();
        $title = $this->input->get('title');
        if (isset($_GET['title']) && !empty($title)) {
            $where['title'] = $title;
        }
        $isrecommend = $this->input->get('isrecommend');
        if (isset($_GET['isrecommend']) && $isrecommend != -1) {
            $where['isrecommend'] = $isrecommend;
        }
        
        $classid = $this->input->get('classid');
        if (isset($_GET['classid']) && $classid != -1) {
            $where['classid'] = $classid;
        }
        
        $isclose = $this->input->get('isclose');
        if (isset($_GET['isclose']) && $isclose != -1) {
            $where['isclose'] = $isclose;
        }
        
        $page = $this->input->get('page'); //当前页
        $page = intval($page);
        if ($page < 1) {
            $page = 1;
        }
        $cnt = $this->m_article->get_count($where);
       
        $page_size = 10;
        $arr = array(
            'page' => $page, //当前页
            'page_size' => $page_size, //分页总大小
            'total' => $cnt//总记录数
        );
        $start_page = ($page - 1) * $page_size;
        $data['pagination'] = new pagination($arr);

        $t_data = array(
            'where' => $where,
            'limit' => $page_size,
            'offset' => $start_page,
            'order' => $orderkey . ' ' . $ascdesc,
        );
        $list = $this->m_article->get_article_list($t_data);
        $this->load->model('m_articlepic');
        foreach($list as $k => $v){
            $list[$k]['cnt'] = $this->m_articlepic->get_count(array('articleid'=>$v['articleid']));
        }
        $data['list'] = $list;
       

        $this->load->model('m_admin');
        $data['admin'] = $this->m_admin->get_all(array('column' => 'adminname,adminid'));
 
        $data['classlist'] = $this->m_class->get_all(array('column' => 'classname,classid'));
        
        $cl_data = array(
            'classpropertyid' => $this->classpropertyid,
        );
        $data['tree'] = $this->m_class->get_class_tree($cl_data);

        $where['orderkey'] = $orderkey;
        $where['ascdesc'] = $ascdesc;
        $data['where'] = $where;
        $this->load->view('article/index', $data);
    }

    function move() {
        $articleid = $this->input->get('articleid');
        $list = explode('.', $articleid);
        $articlelist = $this->m_article->get_article_in_id($list);
        if (empty($articlelist)) {
            // 提示
            $vt_data = array(
                'js_fun' => 'tips',
                'param' => array('请选择移动文章', 'index'),
            );
            $this->load->view('tips', $vt_data);
            return;
        }

        if (isset($_POST['btnSave'])) {
            $classid = intval($this->input->post('classid'));
            $gc_data = array(
                'column' => 'classid,classpropertyid,classname',
                'classid' => $classid,
                'classpropertyid' => $this->classpropertyid
            );
            $class = $this->m_class->get_class($gc_data);

            if (empty($class)) {
                // 提示
                $vt_data = array(
                    'js_fun' => 'tips',
                    'param' => array('请选择目标栏目', 'move?articleid=' . $articleid),
                );
                $this->load->view('tips', $vt_data);
                return;
            }
            foreach ($articlelist as $k => $v) {
                $up_data = array(
                    'articleid' => $v['articleid'],
                    'classid' => $class['classid'],
                );
                $this->m_article->update($up_data);
            }
            redirect('article/index');
        }
        $cl_data = array(
            'classpropertyid' => $this->classpropertyid,
        );
        $v_data['tree'] = $this->m_class->get_class_tree($cl_data);

        $this->load->view('article/move', $v_data);
    }

    public function add() {
        $defalut = array(
            'configid' => 0,
            'classid' => 0,
            'title' => '',
            'author' => '',
            'comefrom' => '',
            'smallpic' => '',
            'bigpic' => '',
            'keywords' => '',
            'description' => '',
            'details' => '',
            'isrecommend' => 0,
            'clicknum' => 1,
            'listid' => 1,
            'addtime' => date('Y-m-d H:i:s'),
            'isclose' => 0
        );

        $articleid = $this->input->get('articleid');
        if ($articleid > 0) {
            //修改
            $article = $this->m_article->get_article(array('articleid' => $articleid));
            if (empty($article)) {
                // 提示
                $vt_data = array(
                    'js_fun' => 'tips',
                    'param' => array('文章不存在', 'index'),
                );
                $this->load->view('tips', $vt_data);
                return;
            }
            $configid = $this->m_class->get_class(array('column' => 'configid', 'classid' => $article['classid']));
            $article['configid'] = $configid['configid'];
            $cl_data = array(
                'configid' => $configid['configid'],
                'classpropertyid' => $this->classpropertyid,
            );
            $data['classtree'] = $this->m_class->get_class_tree($cl_data);
            $data['post_url'] = b_url('article/add?articleid=' . $articleid);
            $data['article'] = $article;
        } else {
            $data['article'] = $defalut;
            $data['post_url'] = b_url('article/add');
        }
        
        if (isset($_POST['btnSave'])) {
            $post = $this->input->post();
            $result = array();
            foreach ($post as $k => $v) {
                if (isset($defalut[$k])) {
                    $defalut[$k] = $post[$k];
                }
            }
            if(!file_exists('.'.$defalut['smallpic'])){
                $defalut['smallpic']='';
            }
            if(!file_exists('.'.$defalut['bigpic'])){
                $defalut['bigpic']='';
            }
            unset($defalut['configid']);
            if ($articleid > 0) {
                $defalut['articleid'] = $article['articleid'];
                $this->m_article->update($defalut);
            } else {
                $defalut['adminid'] = $this->session->userdata['user_id'];
                $this->m_article->insert($defalut);
            }
            redirect('article/index');
            exit;
        }
        $this->load->model('m_config');
        $data['config'] = $this->m_config->get_all(array('column' => 'configid,languagever'));
        $this->load->view('article/add', $data);
    }

    function delete() {
        $articleid = $this->input->get('articleid');
        $list = explode('.', $articleid);
        foreach ($list as $k => $v) {
            $v = intval($v);
            if ($v > 0) {
                $this->m_article->delete(array('articleid' => $v));
            }
        }
        // 提示
        $vt_data = array(
            'js_fun' => 'tips',
            'param' => array('编号为' . str_replace('.', '，', $articleid) . '文章删除成功!', 'index', 1),
        );
        $this->load->view('tips', $vt_data);
    }

    function openclose() {
        $articleid = $this->input->get('articleid');
        $status = $this->input->get('status');
        $str_status = '关闭';
        if ($status != 1) {
            $status = 0;
            $str_status = '开放';
        }
        $list = explode('.', $articleid);
        foreach ($list as $k => $v) {
            $v = intval($v);
            if ($v > 0) {
                $this->m_article->update(array('articleid' => $v, 'isclose' => $status));
            }
        }
        // 提示
        $vt_data = array(
            'js_fun' => 'tips',
            'param' => array('编号为' . str_replace('.', '，', $articleid) . '文章' . $str_status . '成功!', 'index', 1),
        );
        $this->load->view('tips', $vt_data);
    }

    function get_class() {
        $configid = $this->input->get('configid');
        $data = array(
            'configid' => $configid,
            'classpropertyid' => $this->classpropertyid,
        );
        $list = $this->m_class->get_class_tree($data);
        echo json_encode($list);
    }

 

}
